import Req from '@/api/orderManager/index'
import OrderDetail from './detail/index.vue'

export default {
  name: 'ProductMaintenance',
  components: {
    OrderDetail
  },
  data() {
    return {
      // 搜索表单
      searchForm: {
        category: '',
        subCategory: '',
        name: ''
      },
      // 表格数据
      tableData: [],
      // 选中的行
      selectedRows: [],
      // 分页信息
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      // 弹窗显示控制
      dialogVisible: false,
      dialogTitle: '',
      // 表单数据
      formData: {},
      // 表单验证规则
      formRules: {
        category: [
          { required: true, message: '请选择类别', trigger: 'change' }
        ],
        subCategory: [
          { required: true, message: '请选择子类', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入单价', trigger: 'blur' }
        ]
      },
      // 类别选项
      categoryOptions: [
        { value: '服务费', label: '服务费' },
        { value: '寿衣', label: '寿衣' },
        { value: '灵堂', label: '灵堂' },
        { value: '小物件', label: '小物件' },
        { value: '寿盒', label: '寿盒' },
        { value: '鲜花', label: '鲜花' },
        { value: '烧纸碗', label: '烧纸碗' },
        { value: '安葬', label: '安葬' }
      ],
      // 子类选项（根据选择的类别动态变化）
      subCategoryOptions: [],
      param: {
        orderNo: '',
        deceasedName: '',
        pageSize: 10,
        pageNum: 1,
      },
      detailVisible: false,
      currentOrderId: '',
      orderDetail: {},
      discountData: [
        {
          value: 0.9,
          label: '9折'
        },
        {
          value: 0.8,
          label: '8折'
        },
        {
          value: 0.7,
          label: '7折'
        },
        {
          value: 0.6,
          label: '6折'
        },
      ]
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 搜索
    handleSearch() {
      this.param.pageNum = 1
      this.param.pageSize = 10
      // 实现搜索逻辑
      this.getOrderList()
    },
    // 重置搜索
    resetSearch() {
      this.param = {
        orderNo: '',
        deceasedName: '',
      }
      this.handleSearch()
    },
    // 编辑
    handleEdit(row) {
      this.dialogTitle = '编辑订单'
      this.formData = { ...row }
      console.log(this.formData)
      this.dialogVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认删除该产品?', '提示', {
        type: 'warning'
      }).then(() => {
        // 实现删除逻辑
      })
    },
    // 表单提交
    handleSubmit() {
      Req.updateOrderList(this.formData).then(res => {
        if (res.code === 200) {
          this.dialogVisible = false
          this.$message({
            type:'success',
            message: '编辑成功!'
          })
          this.init()
        }
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '编辑失败!'
        })
      })
      // this.$refs.productForm.validate((valid) => {
      //   if (valid) {
      //     // 实现保存逻辑
      //     this.dialogVisible = false
      //   }
      // })
    },
    // 选择变化
    handleSelectionChange(val) {
      this.selectedRows = val
    },
    // 页码变化
    handleCurrentChange(val) {
      this.param.pageNum = this.pagination.currentPage = val
      this.getOrderList()
    },
    // 每页条数变化
    handleSizeChange(val) {
      this.param.pageSize = this.pagination.pageSize = val
      this.getOrderList()
    },
    // 初始化
    init() {
      this.param.pageNum = 1
      this.param.pageSize = 10
      this.getOrderList()
    },
    // 获取订单列表
    getOrderList() {
      Req.getOrderList(this.param).then(res => {
        this.tableData = res.data.list
        this.pagination.total = res.data.total
      })
    },
    formatAmount(row, column, cellValue, index) {
      return `${cellValue.toLocaleString()} 元`;
    },
    // 查看订单详情
    handleViewDetail(orderId) {
      console.log(orderId)
      // this.currentOrderId = orderId
      // this.orderDetail = this.obj.data
      // this.detailVisible = true

      Req.orderDetail(orderId).then(res => {
        this.orderDetail = res.data
        this.detailVisible = true
      })
    },
    onClose() {
      this.detailVisible = false
      this.currentOrderId = ''
    },
    onChangeDiscount(e) {
      this.formData.orderRealAmount = Math.floor(this.formData.orderAmount * e)
    }
  }
}